/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.washington.apl.weather.transforms;

import edu.washington.apl.weather.WeatherStation;
import edu.washington.apl.weather.util.WeatherProperties;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ricky
 */
public class RapidFire extends Transform {

    String stationId;
    String password;
    WeatherProperties properties = WeatherProperties.getInstance();

    @Override
    public String transform(WeatherStation station) {

        // create a request which will go to the Weather Underground 
        StringBuilder str = new StringBuilder("http://rtupdate.wunderground.com/weatherstation/updateweatherstation.php?ID="
                + properties.getProperty("datatarget.rapidfire.stationid", "Not Defined") + "&PASSWORD="
                + properties.getProperty("datatarget.rapidfire.password", "Not Defined") + "");
        double d;
        String s;
        try {
            d = station.getBarometricPressure();
            str.append("&baromin=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getWindDir();
            str.append("&winddir=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getWindAvg();
            str.append("&windspeedmph=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getWindGust();
            str.append("&windgustmph=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getOutdoorHumidity();
            str.append("&humidity=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getOutdoorDewpoint();
            str.append("&dewptf=").append(d);
        } catch (UnsupportedOperationException ex) {
        }

        try {
            d = station.getOutdoorTemp();
            str.append("&tempf=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            d = station.getRainTotal();
            str.append("&dailyrainin=").append(d);
        } catch (UnsupportedOperationException ex) {
        }
        try {
            Timestamp t = new Timestamp(System.currentTimeMillis() + (60 * 60 * 1000 * 5));
            str.append("&dateutc=").append(t.toString());
        } catch (UnsupportedOperationException ex) {
        }

        str.append("&action=updateraw&realtime=1&rtfreq=2.5");
        if (properties.getBooleanProperty("datatarget.rapidfire.debug", false))  {
            System.err.println("*RAPID FIRE DEBUG*> " + str.toString());
        } else {
            try {
                URL url = new URL(str.toString());
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                System.err.println(in.readLine());
                in.close();
            } catch (Exception ex) {
                Logger.getLogger(RapidFire.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return str.toString();
    }
}
